using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using Acms.SqlDal;

namespace Acms.Bll
{
    public class User : BaseItem
    {
        protected User() : base() { }
        protected User(Hashtable table) : base(table) { }
        private User(Hashtable table, List<User> collection) : base(table, collection) { }
        protected User(Hashtable table, ICollection collection) : base(table, collection) { }

        protected override Base CreateDal()
        {
            return new UserDal();
        }
        
        public static User CreateInstance()
        {
            return new User();
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[UserDal.Fields.Id];
            firstName = (string)table[UserDal.Fields.FirstName];
            lastName = (string)table[UserDal.Fields.LastName];
            dateOfBirth = (DateTime)table[UserDal.Fields.DateOfBirth];
            emailAddress = (string)table[UserDal.Fields.EmailAddress];
        }

        protected int id;
        public int Id
        {
            get { return id; }
        }


        protected string firstName;
        public string FirstName
        {
            get{ return firstName;}
            set { firstName = value;}
        }

        protected string lastName;
        public string LastName
        {
            get { return lastName; }
            set { lastName = value; }
        }

        public string DisplayName
        {
            get
            {
                string tmp = string.Format("{0} {1}", firstName, lastName);
                tmp = tmp.Trim();
                while(tmp.Contains("  "))
                {
                    tmp = tmp.Replace("  ", " ");
                }
                return tmp;
            }
        }

        protected DateTime dateOfBirth;
        public DateTime DateOfBirth
        {
            get { return dateOfBirth; }
            set { dateOfBirth = value; }
        }

        protected string emailAddress;
        public string EmailAddress
        {
            get { return emailAddress; }
            set { emailAddress = value; }
        }

        public int Age
        {
            get { return AgeAt(DateTime.Now); }
        }

        public int AgeAt(DateTime dateTime)
        {
            int val = 0;
            if (DateOfBirth != DateTime.MinValue)
            {
                val = dateTime.Year - DateOfBirth.Year;
                if (dateTime.Month < DateOfBirth.Month || (dateTime.Month == DateOfBirth.Month && dateTime.Day < DateOfBirth.Day))
                {
                    val--;
                }
            }
            return val;
        }



        public static List<User> GetAll()
        {
            List<User> tmp = new List<User>();
            UserDal dal = new UserDal();
            Hashtable[] tables = dal.GetAll();
            if(tables != null)
            {
                foreach (Hashtable t in tables)
                {
                    tmp.Add(new User(t, tmp));
                }
            }
            return tmp;
        }

        private static User GetUser(Hashtable table)
        {
            if(table == null)
                return null;
            else
                return new User(table);
        }

        public static User GetById(int id)
        {
            UserDal dal = new UserDal();
            return GetUser(dal.GetById(id));
        }

        public static User GetByUserName(string userName)
        {
            UserDal dal = new UserDal();
            return GetUser(dal.GetByUserName(userName));
        }

        public static User GetByEmailAddress(string emailAddress)
        {
            UserDal dal = new UserDal();
            return GetUser(dal.GetByEmailAddress(emailAddress));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            ((UserDal)dal).SaveChanges(ref id, firstName, lastName, dateOfBirth, emailAddress, ref concurencyId);
        }

        public new void Delete()
        {
            base.Delete();
        }

        protected override void DeleteRecord(object concurencyId, Base dal)
        {
            ((UserDal)dal).Delete(id, concurencyId);
        }

    }
}
